﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;

namespace RSSReader.WebClient.Framework
{
  public class UserIdentity : IIdentity
  {
    #region Private variables

    private readonly FormsAuthenticationTicket _ticket;
    private readonly RssReaderTicketUserData _userData;

    #endregion

    #region Constuctor

    /// <summary>
    /// Initialize the identity with the FormsAuthenticationTicket in order
    /// to read custom userdata.
    /// </summary>
    /// <param name="ticket"></param>
    public UserIdentity(FormsAuthenticationTicket ticket)
    {
      _ticket = ticket;
      _userData = new JavaScriptSerializer().Deserialize<RssReaderTicketUserData>(_ticket.UserData);
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets the AuthenticationType being used.
    /// </summary>
    public string AuthenticationType
    {
      get { return "Forms"; }
    }

    /// <summary>
    /// Gets a flag indicating if the user is Authenticated or not.
    /// </summary>
    public bool IsAuthenticated
    {
      get { return true; }
    }

    /// <summary>
    /// Gets the user name associated with the FormsAuthenticationTicket.
    /// </summary>
    public string Name
    {
      get { return _ticket.Name; }
    }

    /// <summary>
    /// Gets the FormsAuthenticationTicket associated with this User.
    /// </summary>
    public FormsAuthenticationTicket Ticket
    {
      get { return _ticket; }
    }

    /// <summary>
    /// Gets the user's data stored in the FormsAuthenticationTicket.
    /// </summary>
    public RssReaderTicketUserData UserData
    {
      get
      {
        return _userData;
      }
    }

    #endregion
  }
}